package com.eckey.lab.controller;

import com.alibaba.fastjson.JSON;
import com.eckey.lab.config.FreshProperties;
import com.eckey.lab.entity.Order;
import com.eckey.lab.feign.AccountFeign;
import com.eckey.lab.feign.UserFeign;
import com.eckey.lab.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Slf4j
//@RefreshScope
@RestController
@RequestMapping("/order")
public class OrderController {

    @Value("${thread.num}")
    private Integer threadNum;

    @Autowired
    private FreshProperties properties;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserFeign userFeign;


    @Autowired
    private OrderService orderService;


    @GetMapping("/test")
    public String getUserById(@RequestHeader(value = "hello", required = false) String value) {
        String url = "http://user-service/user/findOrderByUserId/";
        String forObject = restTemplate.getForObject(url, String.class);
        log.info("查询结果为：{}", forObject);
        return forObject;
    }

    @GetMapping()
    public Integer get() {
        return properties.getNum();
    }

    @GetMapping("/feign")
    public String getUserById(Integer id) {
        String userById = userFeign.getUserById(id);
        return userById;
    }

    @PostMapping("add")
    public String add(@RequestBody Order order) {
        log.info("新增订单信息：{}", JSON.toJSONString(order));
        orderService.insert(order);
        HashMap<String, Object> maps = new HashMap<String, Object>();
        maps.put("code", "0");
        maps.put("msg", "success");
        return JSON.toJSONString(maps);
    }

    @GetMapping("/getOrderById")
    public String getOrderById(Integer id) {
        Order order = orderService.queryById(id);
        log.info("order:{}", JSON.toJSONString(order));
        HashMap<String, Object> maps = new HashMap<String, Object>();
        maps.put("code", "0");
        maps.put("msg", "success");
        maps.put("data", JSON.toJSONString(order));
        return JSON.toJSONString(maps);
    }

}
